package Servlet.List;

import Server.List.novelListServer;
import bean.userBean;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Arrays;

@WebServlet(name = "novelListManagerServlet", value = "/novelListManagerServlet")
public class novelListManagerServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ServletContext context = this.getServletContext();
        // 通过ServletContext对象获取文件的真实路径
        String realPath = context.getRealPath("");
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        String url="novelListTile.jsp";
        HttpSession session = request.getSession();
        userBean user = (userBean) session.getAttribute("user");
        if(user!=null && user.getUserType().equals("admin")){
            url="novelManagerList.jsp";
        }
        try {
            novelListServer server=new novelListServer(request,response);
            if (request.getParameter("buttonDelete") != null) { 				//如果单击了删除按钮

                String[] novelNumber = request.getParameterValues("novelNumber"); 	//获取ID列表
                System.out.println(Arrays.toString(novelNumber));
                if (novelNumber != null) {
                    server.deleteByIdList(request, novelNumber);						//****删除所选。在本类中定义的方法
                }
            }

            server.queryNovelList();
            server.queryUserBookSelf();

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            request.getRequestDispatcher(url).forward(request, response);		//转发
        }



    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }
}
